package net.sureon.dao;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
@SuppressWarnings("unchecked")
public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {

	public User getUser(long companyID, long userID) {
		User user = new User();
		user.setCompanyID(companyID);
		user.setUserID(userID);
		return (User) getSqlMapClientTemplate().queryForObject("getUserByID",
				user);
	}

	public List<User> getUserByGroup(long companyID, long groupID) {
		User user = new User();
		user.setCompanyID(companyID);
		user.setGroupID(groupID);
		return getSqlMapClientTemplate().queryForList("getUserByGroup", user);
	}

	public List<User> getUserByCompany(long companyID, int skipResults,
			int maxResults) {
		User user = new User();
		user.setCompanyID(companyID);
		return getSqlMapClientTemplate().queryForList("getUserByCompany", user,
				skipResults, maxResults);
	}

	public User getUserByUsername(long companyID, String userName) {
		User user = new User();
		user.setCompanyID(companyID);
		user.setUserName(userName);
		return (User) getSqlMapClientTemplate().queryForObject(
				"getUserByUserName", user);
	}

	public long insertUser(User user) {
		Object o = getSqlMapClientTemplate().insert("insertUser", user);
		if (o instanceof Long) {
			return ((Long) o);
		}
		return 0;
	}

	public int updateUser(User user) {
		return getSqlMapClientTemplate().update("updateUser", user);
	}

	public List<Long> getUserIDByGroup(long companyID, long groupID,
			long exclusiveUserID) {
		User user = new User();
		user.setCompanyID(companyID);
		user.setGroupID(groupID);
		user.setUserID(exclusiveUserID);
		return getSqlMapClientTemplate().queryForList("getUserIDByGroup", user);

	}

	public List<Long> getMemberUserIDByGroup(long companyID, long groupID) {
		User user = new User();
		user.setCompanyID(companyID);
		user.setGroupID(groupID);
		return getSqlMapClientTemplate().queryForList(
				"getMemeberUserIDByGroup", user);
	}

	public User getUserByEmail(String email) {
		User user = new User();
		user.setEmail(email);
		return (User) getSqlMapClientTemplate().queryForObject(
				"getUserByEmail", user);
	}

	public User getAdminUser(long companyID) {
		return (User) getSqlMapClientTemplate().queryForObject("getCompanyAdminUser",
				companyID);
	}
}
